#!/usr/bin/env python
#
# Convert epycsdoc's LaTeX sty files to HTML

from epycsdoc.docwriter.latex_sty import STYLESHEETS
import re, sys, os.path

TEMPLATE = """\
<?xml version="1.0" encoding="ascii" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ascii" />
<title>%(title)s</title>
<link rel="stylesheet" href="epycsdoc.css" type="text/css" />
</head>
<body>
<div class="body">
<h1 class="title">%(title)s</h1>
<pre>%(body)s</pre>
</div>
<table width="100%%" class="navbox" cellpadding="1" cellspacing="0">
  <tr>
  <a class="nav" href="index.html">
    <td align="center" width="20%%" class="nav">
    <a class="nav" href="index.html">
    Home</a></td></a>
  <a class="nav" href="installing.html">
    <td align="center" width="20%%" class="nav">
    <a class="nav" href="installing.html">
    Installing Epydoc</a></td></a>
  <a class="nav" href="using.html">
    <td align="center" width="20%%" class="nav">
    <a class="nav" href="using.html">
    Using Epydoc</a></td></a>
  <a class="nav" href="epytext.html">
    <td align="center" width="20%%" class="nav">
    <a class="nav" href="epytext.html">
    Epytext</a></td></a>
  <td align="center" width="20%%" class="nav">

    <A href="http://sourceforge.net/projects/epycsdoc">
    <IMG src="sflogo.png"
    width="88" height="26" border="0" alt="SourceForge"
    align="top"/></A></td>
    </tr>
</table>
</body>
</html>
"""

COLOR = {'def': '#705000',
         'defname': '#000080',
         'comment': '#005080',
         'command': '#705000',
         'escape': '#ffffff',
         }
COLORIZE_RE = re.compile('|'.join(['(%s)' % s for s in [
    r'(?P<def>(re)?new(command|environment)){(?P<defname>[^}]+)}',
    r'(?P<command>\\\w+)',
    r'(?P<escape>\\.)',
    r'(?P<comment>%.*)',
    ]]))

def subfunc(m):
    if m.group('def') is not None:
        return ('<code class="%s">%s</code>{<code class="%s">%s</code>}' %
                ('keyword', m.group('def'), 'function', m.group('defname')))
    if m.group('command') is not None:
        return '<code class="%s">%s</code>' % ('keyword', m.group('command'))
    if m.group('escape') is not None:
        return '<code class="%s">%s</code>' % ('escape', m.group('escape'))
    if m.group('comment') is not None:
        return '<code class="%s">%s</code>' % ('comment', m.group('comment'))
    assert False, 'expected to match some group'

def colorize(s, title):
    s = s.replace('&', '&amp;')
    s = s.replace('<', '&lt;')
    s = s.replace('>', '&gt;')
    body = COLORIZE_RE.sub(subfunc, s)
    return TEMPLATE % dict(title=title, body=body)

def main():
    if len(sys.argv) != 2:
        print 'Usage: %s <output-dir>' % sys.argv[0]
        sys.exit(-1)

    # hackish to hardcode this; oh well.
    sty_list = open('doc/epycsdoc-style-list.txt', 'w')
    sty_list.write('.. This file is automatically generated by %s\n\n' %
                   sys.argv[0])

    output_dir = sys.argv[1]
    for (name, sheet) in sorted(STYLESHEETS.items()):
        if name == 'default': pass
        filename = 'epycsdoc-sty-%s.html' % name
        title = 'LaTeX Style File: epycsdoc-%s.sty' % name
        out = open(os.path.join(output_dir, filename), 'wb')
        out.write(colorize(sheet, title))
        out.close()
        sty_list.write('- `%s <%s>`__\n' % (title, filename))

    sty_list.close()

    # hackish to hardcode this; oh well.
    demo = open('doc/epycsdoc-latex-demo.tex').read()
    out = open(os.path.join(output_dir, 'epycsdoc-latex-demo.html'), 'wb')
    out.write(colorize(demo, 'Epydoc LaTeX Style Reference'))
    out.close()


if __name__ == '__main__':
    main()
